//
//  YJWTabBarController.m
//  百思不得骑姐
//
//  Created by YJW on 16/10/9.
//  Copyright © 2016年 yijiang. All rights reserved.
//

#import "YJWTabBarController.h"

#import "YJWEssenceViewController.h"
#import "YJWNewViewController.h"
#import "YJWFriendViewController.h"
#import "YJWMeViewController.h"

#import "YJWNavController.h"
#import "YJWTabBar.h"

@interface YJWTabBarController ()

@end

@implementation YJWTabBarController

+(void)initialize{
    
    //通过 appearance 一次性设置所有 tabBar 的所有字体属性
    UITabBarItem *item = [UITabBarItem appearance];
    
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    attr[NSForegroundColorAttributeName] = [UIColor orangeColor];
    
    NSMutableDictionary *selectedAttr = [NSMutableDictionary dictionary];
    selectedAttr[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    selectedAttr[NSForegroundColorAttributeName] = [UIColor redColor];
    
    [item setTitleTextAttributes:attr forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttr forState:UIControlStateSelected];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    [self createControllerWithController:
     [[YJWEssenceViewController alloc] init] title:@"热门" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];
    
    [self createControllerWithController:[[YJWNewViewController alloc] init] title:@"最新" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];
    
    [self createControllerWithController:[[YJWFriendViewController alloc] init] title:@"关注" image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon"];
    
    [self createControllerWithController:[[YJWMeViewController alloc] init] title:@"我" image:@"tabBar_me_icon" selectedImage:@"tabBar_me_click_icon"];
    
    /*自定义 tabBar，但是 tabBar 是只读属性，所以用 KVC 进行设置
    *   self.tabBar = [[UITabBar alloc]init];
    */
    [self setValue:[[YJWTabBar alloc] init] forKeyPath:@"tabBar"];
//    [self setValue:[[YJWTabBar alloc] init] forKey:@"tabBar"];
}

-(void)createControllerWithController:(UIViewController*)vc title:(NSString*)title image:(NSString*)image selectedImage:(NSString*)selectedImage{
    
    /**
     *不可以在此处设置背景色，会在程序启动时就加载所有界面数据
     */
    //vc.view.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];
    
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:image];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    
    YJWNavController *nav = [[YJWNavController alloc] initWithRootViewController:vc];
    
    [self addChildViewController:nav];
}

#pragma mark 创建控制器,后进行封装优化
-(void)createController{
    UIViewController *viewController1 = [[UIViewController alloc] init];
    viewController1.view.backgroundColor = [UIColor redColor];
    viewController1.tabBarItem.title = @"最热";
    viewController1.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
    viewController1.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"];
    
    UIViewController *viewController2 = [[UIViewController alloc] init];
    viewController2.view.backgroundColor = [UIColor purpleColor];
    viewController2.tabBarItem.title = @"最新";
    viewController2.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"];
    viewController2.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_new_click_icon"];
    
    UIViewController *viewController3 = [[UIViewController alloc] init];
    viewController3.view.backgroundColor = [UIColor yellowColor];
    viewController3.tabBarItem.title = @"关注";
    viewController3.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"];
    viewController3.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_friendTrends_click_icon"];
    
    UIViewController *viewController4 = [[UIViewController alloc] init];
    viewController4.view.backgroundColor = [UIColor grayColor];
    viewController4.tabBarItem.title = @"我";
    viewController4.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"];
    viewController4.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_me_click_icon"];
    
    self.viewControllers = @[viewController1,viewController2,viewController3,viewController4];
}

#pragma mark 设置 tabBarItem 的字体和图片属性(比较麻烦的一种方法)
-(void)setting1{
    
    UIViewController *vc01 = [[UIViewController alloc] init];
    
    //设置字体
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    attr[NSForegroundColorAttributeName] = [UIColor grayColor];
    
    NSMutableDictionary *selectedAttr = [NSMutableDictionary dictionary];
    selectedAttr[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    selectedAttr[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    
    [vc01.tabBarItem setTitleTextAttributes:attr forState:UIControlStateNormal];
    [vc01.tabBarItem setTitleTextAttributes:selectedAttr forState:UIControlStateSelected];
    
    //设置图片为原来颜色
    UIImage *img = [UIImage imageNamed:@"tabBar_essence_click_icon"];
    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [vc01.tabBarItem setSelectedImage:img];
}

@end
